home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1105 < prev    next >
Encoding:
Text File  |  1996-08-06  |  782 b   |  32 lines

  1. Path: iona.ie!this
  2. From: akohli@iona.com (Aman S Kohli)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to print a enum variable's names
  5. Date: 9 Jan 1996 10:02:47 GMT
  6. Organization: Iona Technologies
  7. Message-ID: <4cteg7$16r@pointer.dublin.iona.ie>
  8. NNTP-Posting-Host: this.dublin.iona.ie
  9. X-Newsreader: News Xpress Version 1.0 Beta #4
  10.  
  11. Hi,
  12.  
  13.     Why not just overload the << operator? Here's a stupid example: ( All 
  14. copyrights are willfully acknowledged ):
  15.  
  16. #include <iostream.h>
  17.  
  18. enum Flinstones { Fred, Willma, Pebbles };
  19. ostream& operator<<( ostream& _strm, Flinstones f ) {
  20.     static char * enumValue[] = { "Fred", "Willma", "Pebbles" };
  21.     _strm << enumValue[ f ];
  22.     return _strm; 
  23. }
  24.  
  25. main() {
  26.     Flinstones MyFavorite = Pebbles;
  27.     cout << MyFavorite << " is my favorite Flinstone.\n";
  28. }
  29.  
  30.  
  31. Enjoy
  32.